package com.jolink.omiis.wechat.util;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * XML格式转换辅助工具类
 * @author WangYanqing
 *
 */
public class XmlParser {
	private static Logger logger = LoggerFactory.getLogger(XmlParser.class);

	private XmlParser() {
	}

	/**
	 * 将xml格式的-str转为dom
	 * @param xmlStr
	 * @return
	 */
	public static Document str2Xml(String xmlStr) {
		return str2Xml(new StringReader(xmlStr));
	}

	/**
	 * 将reader转为dom
	 * @param reader
	 * @return
	 */
	public static Document str2Xml(Reader reader) {
		Document doc = null;
		SAXReader loader = new SAXReader();
		try {
			doc = loader.read(reader);
		} catch (DocumentException e) {
			logger.error(e.getMessage(), e);
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				logger.error(e.getMessage(), e);
			}
		}
		return doc;
	}
}
